import { Component } from 'react'
import Taro from '@tarojs/taro'
import { View,Text,ScrollView} from '@tarojs/components'
import { connect } from 'react-redux'
import { LoadStudentActionAsync,DeleteStudentActionAsync,AddStudentActionAsync} from '../../redux/action/studentAction';
import { ChangeLoadingAction } from '../../redux/action/loadingAction';
import Loading from '../../components/Loading/index'
import dev from '../../../config/dev';
import http from '../../service/http'
import './index.less'

const isH5 = process.env.TARO_ENV === 'h5'

@connect(	state => ({
    studentList:state.studentList
	}),
	{LoadStudentActionAsync,DeleteStudentActionAsync,AddStudentActionAsync,ChangeLoadingAction}
)
class orderList extends Component {
  state={
    list:[],
    pageNum:1,
    pageLast:0
  }

  // UNSAFE_componentWillMount () { console.log('UNSAFE_componentWillMount,组件还没挂载前');if(isH5){console.log('你当前环境为H5')} }

  // componentDidMount () { console.log('componentDidMount,组件挂载后');}

  // shouldComponentUpdate (nextProps,nextState) { console.log('shouldComponentUpdate,组件是否需要更新',nextProps,nextState);return true }

  // componentWillUnmount () { console.log('componentWillUnmount,组件卸载前') }

  // componentDidShow () { console.log('componentDidShow,组件显示后') }

  // componentDidHide () { console.log('componentDidHide,组件隐藏后') }

    // onLoad
  async onLoad (options) {
    let openId=Taro.getStorageSync('openId')
    Taro.showLoading({
      title: '加载中',
    })
    let {data}=await http.pageListOrder({custom:openId})
    this.setState({list:data.list,pageLast:data.pages})
    Taro.hideLoading()
  }

  // onReady
  onReady () {}

  // 对应 onShow
  componentDidShow () {}

  // 对应 onHide
  componentDidHide () {}

  // 对应 onPullDownRefresh，除了 componentDidShow/componentDidHide 之外，
  // 所有页面生命周期函数名都与小程序相对应
  onPullDownRefresh () {}

  ScrollToLower=async()=>{
    let that=this
    let pageNum=this.state.pageNum+1
    if(pageNum<=this.state.pageLast){
      let openId=Taro.getStorageSync('openId')
      Taro.showLoading({
        title: '加载中',
      })
      this.setState({pageNum:(that.state.pageNum+1)})
      let {data}=await http.pageListOrder({custom:openId,pageNum:pageNum})
      console.log({...that.state.list,...data.list})
      this.setState({list:[...that.state.list,...data.list]})
      Taro.hideLoading()
    }else{
      Taro.showToast({
        title:'已是最后一页',
        icon:'none'
      })
    }
  }
  render () {
    return (
      <ScrollView style={{height:'100vh',padding:'0 40rpx'}}
        enhanced
        showScrollbar={false}
        scrollY
        onScrollToLower={this.ScrollToLower}
      >
        <Loading></Loading>
        {this.state.list.length>0?this.state.list.map((item)=>{
          return <View key={item.id}
            style={{
              position:'relative',
              // border:'1rpx #000 solid',
              borderRadius:'20rpx',
              padding:'20rpx 10rpx',
              marginBottom:'20rpx',
              width:'100%',
              backgroundColor:'#1989fa',
              color:'#fff'
            }}
            onClick={()=>{
              let that=this
              Taro.navigateTo({
                url: '/pages/orderDetailed/index?orderId='+item.id
              })
            }}
          >
            <View style={{fontSize:'35rpx'}}>订单编号：{item.id}</View>
            <View style={{marginTop:'10rpx',fontSize:'35rpx'}}>订餐时间：{item.time}</View>
            <View style={{marginTop:'10rpx',fontSize:'35rpx'}}>订餐人数：{item.peopleNum}</View>
            <View style={{marginTop:'10rpx',fontSize:'35rpx'}}>订餐金额：{item.money}元</View>
          </View>
        }):<View style={{
          textAlign:'center'
        }}
        >暂无订单</View>}
      </ScrollView>
    )
  }
}
orderList.defaultProps = {}
export default orderList

